package com.jrx.anytxn.transaction.controller.cms;


import com.jrx.anytxn.common.exception.TxnException;
import com.jrx.anytxn.i18n.response.TxnPage;
import com.jrx.anytxn.i18n.response.TxnRespResult;
import com.jrx.anytxn.transaction.dto.chargeoff.TlChargeoffRep;
import com.jrx.anytxn.transaction.dto.chargeoff.TlChargeoffRes;
import com.jrx.anytxn.transaction.service.IChargeoffService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jrx.auth.filter.service.InfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 与页面相关的核销处理接口
 *
 * @author lbz
 * date 2020/2/3
 * @version V1.0
 **/
@Api(tags = "与页面相关的核销处理接口")
@RestController
@RequestMapping(value = "/transaction/chargeoff")
public class ChargeoffController {
    private static final Logger logger = LoggerFactory.getLogger(ChargeoffController.class);

    @Resource
    private InfoService infoService;
    @Autowired
    private IChargeoffService iChargeoffService;

    @ApiOperation(value = "创建核销订单", notes = "创建核销订单")
    @PostMapping("/")
    public TxnRespResult<TlChargeoffRes> createChargeoff(@RequestBody TlChargeoffRep chargeoffRep){
        TxnRespResult<TlChargeoffRes> result = new TxnRespResult<>();
        TlChargeoffRes tlChargeoffRes;
        try{
            String tenantId = String.valueOf(infoService.getUser().getProjectId());
            chargeoffRep.setTenantId(tenantId);
            chargeoffRep.setOperator(infoService.getUser().getUserName());
            chargeoffRep.setUpdateBy(infoService.getUser().getUserName());
            tlChargeoffRes = iChargeoffService.addChargeoff(chargeoffRep);
            result.getSuccess(tlChargeoffRes);
        } catch (TxnException e) {
            logger.error(e.getMessage(),e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "根据主键ID查询核销信息", notes = "根据主键ID查询核销信息")
    @GetMapping(value = "/getChargeoffById/{id}/{customerId}")
    public TxnRespResult<TlChargeoffRes> getChargeoffById(@PathVariable("id") Long id,
                                                          @PathVariable("customerId") String customerId){
        TxnRespResult<TlChargeoffRes> result = new TxnRespResult<>();
        String tenId = String.valueOf(infoService.getUser().getProjectId());
        TlChargeoffRes tlChargeoffRes;
        try{
            tlChargeoffRes = iChargeoffService.getChargeoffById(id,customerId,tenId);
            result.getSuccess(tlChargeoffRes);
        } catch (TxnException e) {
            logger.error(e.getMessage(),e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "根据订单id查询核销信息", notes = "根据订单id查询核销信息")
    @GetMapping(value = "/getChargeoffByLoanId/{loanId}/{pageNum}/{pageSize}")
    public TxnRespResult<TxnPage<TlChargeoffRes>> getChargeoffByLoanId(@PathVariable("loanId") String loanId,
                                                                       @PathVariable("pageNum") Integer pageNum,
                                                                       @PathVariable("pageSize") Integer pageSize){
        TxnRespResult<TxnPage<TlChargeoffRes>> result = new TxnRespResult<>();
        String tenId = String.valueOf(infoService.getUser().getProjectId());
        TxnPage<TlChargeoffRes> tlChargeoffRes;
        try{
            tlChargeoffRes = iChargeoffService.getChargeoffByLoanId(loanId,tenId,pageNum,pageSize);
            result.getSuccess(tlChargeoffRes);
        } catch (TxnException e) {
            logger.error(e.getMessage(),e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "根据订单id查询核销信息", notes = "根据订单id查询核销信息")
    @GetMapping(value = "/processChargeoffByLoanId/{loanId}")
    public TxnRespResult<TlChargeoffRes> processChargeoffByLoanId(@PathVariable("loanId") String loanId){
        TxnRespResult<TlChargeoffRes> result = new TxnRespResult<>();
        String tenId = String.valueOf(infoService.getUser().getProjectId());
        TlChargeoffRes tlChargeoffRes;
        try{
            tlChargeoffRes = iChargeoffService.processChargeoffByLoanId(loanId,tenId);
            result.getSuccess(tlChargeoffRes);
        } catch (TxnException e) {
            logger.error(e.getMessage(),e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        return result;
    }


    @ApiOperation(value = "修改核销订单核销原因", notes = "修改核销订单核销原因")
    @PutMapping("/")
    public TxnRespResult<TlChargeoffRes> editChargeoff(@RequestBody TlChargeoffRep chargeoffRep){
        TxnRespResult<TlChargeoffRes> result = new TxnRespResult<>();
        TlChargeoffRes tlChargeoffRes;
        try{
            tlChargeoffRes = iChargeoffService.editChargeoff(chargeoffRep);
            result.getSuccess(tlChargeoffRes);
        } catch (TxnException e) {
            logger.error(e.getMessage(),e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "删除核销订单", notes = "删除核销订单")
    @DeleteMapping(value = "/{tenantId}/{id}/{customerId}")
    public TxnRespResult<Boolean> removeByTenIdAndLoanId(@PathVariable String tenantId, @PathVariable Long id,
                                                         @PathVariable String customerId) {
        TxnRespResult<Boolean> result = new TxnRespResult<>();
        Boolean flag;
        try {
            flag = iChargeoffService.removeByTenIdAndLoanId(String.valueOf(infoService.getUser().getProjectId()),customerId, id);
            result.getSuccess(flag);
        } catch (TxnException e) {
            logger.error(e.getMessage(),e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        return result;
    }
}
